package cn.edu.scau.cmi.zhouxinyue.composite;

import javafx.fxml.FXML;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.control.Label;

import javafx.scene.control.TextArea;

public class compositeController {
	@FXML
	private Button pin;
	@FXML
	private Button tin;
	@FXML
	private Button pde;
	@FXML
	private Button tde;
	@FXML
	private Label tip;
	@FXML
	private TextField mes;
	@FXML
	private Button in;
	@FXML
	private TextArea out;
	@FXML
	private Label tip2;
	@FXML
	private TextField mes2;
	
	private String name,fname;
	private Composite root,father,find,newteam;
	private static int choose=0;
	private static Composite rootR=new Composite("root",null);
	
	public void sout(Composite root){
		if(root.getParent()!=null) {
			out.appendText(root.getName()+"(该队伍属于:"+root.getParent().getName()+")  ");
		}
		else {
			out.appendText(root.getName()+'\n');
		}
	    if(root.getTnumber()!=0) {
			int i=0;
			while(i<root.getTnumber()) {
				out.appendText(""+'\n');
				sout((Composite)root.getTeam().get(i));
				i++;
			}
		}
		if(root.getPnumber()!=0) {
			int i=0;
			while(i<root.getPnumber()) {	
				out.appendText(root.getPerson().get(i).getName()+"(该个人属于:"+root.getName()+")  ");
				i++;
			}
		}
	    return ;
	}
	
	public Composite search(Composite root,String name) {
		if(root.getName().equals(name)) {
			find=(Composite)root;
		}
		else if(root.getTnumber()!=0) {
			int i=0;
			while(i<root.getTnumber()) {
				search((Composite)root.getTeam().get(i),name);
				i++;
			}
		}
		return find;
	}
	
	public Composite searchperson(Composite root,String name) {
		if(root.getPnumber()!=0) {
			int i=0;
			while(i<root.getPnumber()) {
				if(root.getPerson().get(i).getName().equals(name)) {
					find=root;
				}
				i++;
			}
		}
		else {
			int i=0;
			while(i<root.getTnumber()) {
				searchperson((Composite)root.getTeam().get(i),name);
				i++;
			}
		}
		return find;	
	}
	
	Leaf newperson=null;
	public compositeController(){
		root=rootR;
		father=null;
	}

	
	public void ppin() {
		choose=1;
		tip.setText("请输入要插入的个人名字:");			
		tip2.setText("请输入要插入的父节点名字");
	}
	
	public void ttin() {
		choose=2;
		tip.setText("请输入要插入的团队名字:");
		tip2.setText("请输入要插入的父节点名字:");
	}
	
	public void ppde() {
		choose=3;
		tip.setText("请输入要删除的个人名字");
		tip2.setText("不用输入");
	}
	
	public void ttde() {
		tip.setText("请输入要删除的团队名字");
		tip2.setText("不用输入");
		choose=4;
	}
	
	public void work() {
		switch(choose) {
		case 1:
			name=mes.getText();
			fname=mes2.getText();
			father=search(root,fname);
			newperson=new Leaf(name,father);
			father.addPerson(newperson);
			break;
		case 2:
			name=mes.getText();
			fname=mes2.getText();
			father=search(root,fname);
			newteam=new Composite(name,father);
			father.addTeam(newteam);
			break;
		case 3:
			name=mes.getText();
			father=searchperson(root, name);
			father.deletePerson(new Leaf(name,null));
			break;
		case 4:
			name=mes.getText();
			father=search(root, name);
			father.getParent().deleteTeam(new Composite(name,null));
			break;
		}
		sout(rootR);
	}

}
